<?php


namespace App\Controller\App;


use App\Base\BaseController4App;
use App\Entity\SalesUserJuan;
use App\Entity\User;
use App\Repository\BannerRepository;
use App\Repository\MoneyRepository;
use App\Repository\SalesCutRepository;
use App\Repository\SalesJuanRepository;
use App\Repository\SalesUserJuanRepository;
use App\Repository\SiteOilCategoryRepository;
use App\Repository\SiteRepository;
use App\Repository\UserRepository;
use App\Service\MoneyService;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/app/user")
 */
class AppUserController extends BaseController4App
{
    /**
     * @Route("/info", name="AppUserInfo")
     */
    public function AppSalesMyJuan(
        UserRepository $userRepository
        , MoneyService $moneyService
        , SalesUserJuanRepository $salesUserJuanRepository
    )
    {
        $user = $this->needToBeUser();
        $data = [];
        $data["user"] = $user;
        $data["password"] = $user->getPassword()?1:0;

        $data["money"] = $moneyService->getUserMoney($user);
        $cond["user"] = $user;
        $cond["status"]=1;
        $cond["expireAt"]['$gt'] = new \DateTime();
        $salesJuans = $salesUserJuanRepository->findBy($cond, array("id"=>"DESC"));
        $data["juan"] = count($salesJuans);

        $this->setGroups("user","app","juan");
        return $this->json_out(1,"OK",$data);
    }

    /**
     * @Route("/bind/phone", name="AppUserPhone")
     */
    public function AppUserPhone(
        UserRepository $userRepository
        ,SalesJuanRepository $salesJuanRepository
    )
    {
        $user = $this->needToBeUser();
        $req = $this->getRequestData();
        $phone = $req["phone"]??"";
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        if (!$phone) {
            return $this->json_out(0,"请输入手机号");
        }
        $first = false;
        if (!$user->getPhone()) {
            $first = true;
        }
        $user->setPhone( $phone);

        $this->myEm()->persist($user);
        $this->myEm()->flush();

        if ($first) {

            //发放优惠券
//            cat = 10  绑定手机奖励
            $juan = $salesJuanRepository->findOneBy(array("cat.id"=>10,"isUse"=>true));
            $userjuan = new SalesUserJuan();
            $userjuan->setStatus(0)
                ->setCreateAt(new \DateTime())
                ->setUser($user)
                ->setJuan($juan)
                ->setExpireAt(new \DateTime(strtotime("+ {$juan->getExpireDay()} days")))
            ;

            $this->myEm()->persist($userjuan);
            $this->myEm()->flush();
        }


        return $this->json_out(1,"修改成功");
    }

    /**
     * @Route("/bind/password", name="AppUserPassword")
     */
    public function AppUserPassword(
        UserRepository $userRepository
    )
    {
        $user = $this->needToBeUser();
        $req = $this->getRequestData();
        $new = $req["new"]??"";
        $phone = $req["phone"]??"";
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        if (!$phone) {
            return $this->json_out(0,"请输入手机号");
        }

        $user->setPassword($new);
        $this->myEm()->persist($user);
        $this->myEm()->flush();

        return $this->json_out(1,"修改成功");
    }

    /**
     * @Route("/check/password", name="AppUserCheckPassword")
     */
    public function AppUserCheckPassword(
        UserRepository $userRepository
    )
    {
        $user = $this->needToBeUser();
        $req = $this->getRequestData();
        $old = $req["password"]??"";
        if (!$user->getPassword()) {
            return $this->json_out(0,"请设置支付密码");
        }
        if ($old && $user->getPassword() && $user->getPassword() != $old){
            return $this->json_out(0,"密码错误");
        }
        return $this->json_out(1,"验证成功");
    }
}